package com.zhiyou.app.config.typehandler;

import com.zhiyou.app.pojo.enums.OrderStatusEnum;
import com.zhiyou.app.pojo.enums.TaskStatusEnum;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 消息类型处理器
 * @author jonathan.z
 */
@MappedTypes(TaskStatusEnum.class)
@Component
public class TaskStatusHandler extends BaseTypeHandler<TaskStatusEnum> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, TaskStatusEnum parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter.getCode());
    }

    @Override
    public TaskStatusEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String code = rs.getString(columnName);
        return TaskStatusEnum.parse(code);
    }

    @Override
    public TaskStatusEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String code = rs.getString(columnIndex);
        return TaskStatusEnum.parse(code);
    }

    @Override
    public TaskStatusEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String code = cs.getString(columnIndex);
        return TaskStatusEnum.parse(code);
    }
}
